package com.lagou.niancheng.core.config;

import com.lagou.niancheng.core.result.ResultSetConverter;
import com.lagou.niancheng.core.session.Executor;
import com.lagou.niancheng.core.session.ExecutorFactory;
import com.lagou.niancheng.core.transaction.TransactionManager;
import com.lagou.niancheng.core.transaction.TransactionManagerFactory;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

public class Configuration {

    private DataSource dataSource;

    /**
     * 所有sql语句对应的方法封装集合
     * key：statementId 即：namespace.方法名
     * value：方法对应的sql语句
     */
    private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();

    private TransactionManagerFactory transactionManagerFactory;

    private TransactionManager transactionManager;

    private ResultSetConverter resultSetConverter;

    private Boolean autoCommit;

    private ExecutorFactory executorFactory;


    public Boolean getAutoCommit() {
        return autoCommit;
    }

    public void setAutoCommit(Boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    public ResultSetConverter getResultSetConverter() {
        return resultSetConverter;
    }

    public void setResultSetConverter(ResultSetConverter resultSetConverter) {
        this.resultSetConverter = resultSetConverter;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public TransactionManagerFactory getTransactionManagerFactory() {
        return transactionManagerFactory;
    }

    public TransactionManager getTransactionManager() {
        if (transactionManager != null) {
            return transactionManager;
        } else {
            transactionManager = transactionManagerFactory.createTransactionManager(this);
        }
        return transactionManager;
    }

    public void setTransactionManagerFactory(TransactionManagerFactory transactionManagerFactory) {
        this.transactionManagerFactory = transactionManagerFactory;
    }

    public ExecutorFactory getExecutorFactory() {
        return executorFactory;
    }

    public void setExecutorFactory(ExecutorFactory executorFactory) {
        this.executorFactory = executorFactory;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }
}
